package kGeom;
import java.util.Random;



public class kUtil {
	public static Random randGen = new Random();

	public static String brandingString = "kGeom Library v1.1x by Kyle Steinfeld \nwww.ksteinfe.net \n________________________________";

	/*
	 * PROJECTION
	 * 
	 * 
	 * 
	 */

	/*
	public static kPoint projectPointToElement( kPoint pt, kElement e ){
		Class c = e.getClass();

		if (c == kTuple.class) {
			return new kPoint( (kTuple) e );
		} else if (c == kPoint.class){
			return kPoint.clone((kPoint) e);
		} else if (c == kVec.class){
			kVec vec = (kVec) e;
			float dist = kVec.projectedLength( new kVec(pt), vec );
			kVec v = kVec.resize(vec, dist);
			return kPoint.translate(pt, v);			
		} else if (c == kLine.class){
			kLine line = (kLine) e;

			// translate point p to origin relitive to line startPoint
			kPoint dp = kPoint.translate(pt, new kVec( line.pt, new kPoint()));
			// find the projected distance
			float dist = kVec.projectedLength(new kVec(new kPoint(), dp), line.vec);

			// if it lies outside the bounds of the line, then return an endpoint
			if (dist < 0) return line.pt;
			if (dist > line.Length()) return line.EndPoint();

			return kPoint.translate(pt, kVec.resize(line.vec, dist));			

		} else if (c == kRange.class){
			// TODO: make projectPointToElement() for kRange
			return new kPoint();

		} else if (c == kPointCloud.class){
			return pt.getNearestPoint( ((kPointCloud) e).getPts() );
		} else {
			 System.out.println("element type not recognized");
			return new kPoint();
		}

	}
	 */

	/*
	 * MATH FUNCTIONS
	 * 
	 * 
	 * 
	 */

	public static float randFloat(){
		return ((float) randGen.nextInt(1000)) /1000f;
	}

	public static float randFloat(  float minV, float maxV ){
		float dif = maxV - minV;
		return ((((float) randGen.nextInt(1000)) /1000f)*dif)+minV;
	}

	public static float round( float f , int perc ){
		float mult = perc * 10f;
		return Math.round(f*mult)/mult;
	}


	public static byte floatToByte(float val) {
		val = (val * 256) - 128;
		if (val >= 128) val = 127;
		if (val <= -128) val = -128;
		return (byte) val;
	}

	public static float byteToFloat(byte val) {
		return (float) (val + 128) / 256;
	}

	public static  byte intToByte(int val) {
		val = val - 128;
		if (val >= 128) val = 127;
		if (val <= -128) val = -128;
		return (byte) val;
	}

	public static  int byteToInt(byte val) {
		return val + 128;
	}


	/*
	 * CONVERSION
	 * 
	 * 
	 * 
	 */

	public static float toFloat(String s){
		return Float.valueOf(s).floatValue();  
	}

	public static int toInt(String s){
		return Integer.valueOf(s).intValue();  
	}



}
